<?php





namespace app\shop\model;



use app\crm\model\FollowRecord;

use app\admin\model\Common;

use think\facade\Db;

use think\model\concern\SoftDelete;

use app\common\traits\ActionRecordTrait;

use app\shop\model\RefundPayModel;




/**

 * 订单模型

 * @package app\shop\model

 * @author LIU

 * @create 2021-10-01

 */

class ShopAmountTJ extends Common

{

    use ActionRecordTrait;



    /**

     * 表名

     * @var string

     */

    protected $name = 'shop_refund_amount';

    //protected $defaultSoftDelete = 0;

	
	/* 
	@  
	*/
	
	public function setShopAmountTj($shopId,$delTime,$orderType,$businessType){
		
		$where = [];
		array_push($where, ['type', '=', 1]);//只有直营店数据
		array_push($where, ['status', '=', 1]);//门店关闭状态
		array_push($where, ['shop_id', '=', $shopId]);// 去掉营销中心
		$shopList = Db::name('shop')->where($where)->column('shop_id,shop_name,shop_type,market_id');
		
		$startTime = strtotime($delTime);
		$endTime   = strtotime($delTime)+86399;
		
		echo '开始时间--'.$startTime;echo '<br/>';
		echo '结束时间--'.$endTime;echo '<br/>';
		echo '门店ID --'.$shopId;echo '<br/>';
		echo '成交时间 --'.$delTime;echo '<br/>';
		echo '成交类型 --'.$orderType;echo '<br/>';
		echo '业务类型 --'.$businessType;echo '<br/>';
		
		//实收金额
		$shopAmountAll = $this-> setshopAmountMoney($shopId,$startTime,$endTime);
		
		if($shopList[0]['market_id'] == 8 || $shopList[0]['market_id'] == 9 || $shopList[0]['market_id'] == 10|| $shopList[0]['market_id'] == 11)
		{
			$referralAmount='0.00';  //转诊金额
			
			$newsXDMoney = $this->sumShopPayMoneyNewMZ('XD',$shopId,$shopList[0]['shop_type'],$startTime,$endTime); //新单金额
			$budanMoney = $this->sumShopPayMoneyNewMZ('BD',$shopId,$shopList[0]['shop_type'],$startTime,$endTime);  //补单金额
			
			$scarMoney =$this->sumShopScarPayMoneyMZ('Bh',$shopId,$shopList[0]['shop_type'],$startTime,$endTime);   //疤痕金额
			$projectMoney =$this->sumShopScarPayMoneyMZ('XM',$shopId,$shopList[0]['shop_type'],$startTime,$endTime);  //项目金额
			
		}else{
			
			$referralAmount=$this-> referralMoney($shopId,$shopList[0]['shop_type'],$startTime,$endTime);  //转诊金额
			
			$newsXDMoney = $this->sumShopPayMoneyNew('XD',$shopId,$shopList[0]['shop_type'],$startTime,$endTime);  //新单金额
			$budanMoney = $this->sumShopPayMoneyNew('BD',$shopId,$shopList[0]['shop_type'],$startTime,$endTime);   //补单金额
			
			$scarMoney =$this->sumShopScarPayMoney('Bh',$shopId,$shopList[0]['shop_type'],$startTime,$endTime);   //疤痕金额
			//项目业绩计算
			$projectMoney =$this->sumShopScarPayMoney('XM',$shopId,$shopList[0]['shop_type'],$startTime,$endTime);  //项目金额
		}
		
		
		echo '<pre>';
		print_r($shopAmountAll);
		
		echo '<pre>';
		print_r($referralAmount); 
		
		/* $list['shop_id']= $shopId;
		$list['total_amount']= bcadd($newsXDMoney,$budanMoney,2);
		$list['new_single']= $newsXDMoney;
		$list['repairOrder']= $budanMoney;
		$list['scar_amount']= $scarMoney;
		$list['project_amount']= $projectMoney;
		$list['referral_amount']= $referralAmount;          //转诊金额
		$list['amount_money']= $shopAmountAll;              //实收金额
		$list['create_time']= $startTime;     */                //成交时间
		
		 $list =[
			 'id'=> '',
			 'shop_id'=> '73782774834649',
		 ];
		echo '<pre>';
		print_r($list);
		
		// 查询当天该门店是否 有数据
		$where = [];
		array_push($where, ['create_time', '>=', $startTime]);//只有直营店数据
		array_push($where, ['create_time', '<=', $startTime]);//门店关闭状态
		array_push($where, ['shop_id', '=', $shopId]);// 去掉营销中心
		$shopAmountT = Db::name('shop_refund_amount')->where($where)->column('shop_id,total_amount,scar_amount');
		
		if(empty($shopAmountT)){
			echo '数据新增';
			$id = Db::name('shop_refund_amount')->insertGetId($list);
			echo '3333--'.$id;
		}else{
			//$bool = $this->where(['id'=>$id])->update($data);
			echo '数据修改';
		}
		
		
		
		
		exit;
	}
	
	
	/* 
	@ 实收金额的逻辑方法运算 
	
	@ 计算逻辑   收款单的收款类型是收款，且收款方式不含店转的收款本金合计 
	           【减去】 退款单中 退款类型是退款 退款方式不含店转的退款本金合计 的差额即为实收业绩；
			   
	*/
	
	public function setshopAmountMoney($shopId,$startTime,$endTime){
		
		//收款单 收款类型是 收款 且不含收款方式不是店转的
		$receive = Db::name('shop_refund_order')->alias('r')
		
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
		    
		    // ->field('sum(r.pay_amount * r.coefficient) as pay_amount')
		    
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where('g.refund_mode','<>','06')
		    
		    ->where('r.type','=',3)
		        
		    ->where('r.status','=',2)
		    
		    ->where('r.refund_type','=','101.02')
		    
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		    
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		    
		        return $query->whereTime('deal_time', '<=', $endTime);
		    
		    },function ($query) use ($endTime) {
		    
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    // ->find();
		
		    ->column('r.pay_amount as pay_amount','r.id');
			//->column('r.pay_amount * r.coefficient as pay_amount','r.id');
		
		//var_dump($receive);
		//退款单	  
		$refund = Db::name('shop_refund_order')->alias('r')
		
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
		    
		    // ->field('sum(r.pay_amount * r.coefficient) as pay_amount')
		    
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where('g.refund_mode','<>','06')
		    
		    ->where('r.type','=',1)
		        
		    ->where('r.status','=',2)
		    
		    ->where('r.refund_type','=','101.02')
		    
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		    
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		    
		        return $query->whereTime('deal_time', '<=', $endTime);
		    
		    },function ($query) use ($endTime) {
		    
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    // ->find();
		
		    //->column('r.pay_amount * r.coefficient as pay_amount','r.id');
			->column('r.pay_amount  as pay_amount','r.id');
		
		// return round($receive['pay_amount'] -$refund['pay_amount'],2);
		
		$receive_sum = array_sum($receive);
		
		$refund_sum = array_sum($refund);
		
		return bcsub($receive_sum,$refund_sum,2);
		
	}
	
	/**
	
	 * @author ran
	
	 * @date 2022-03-09 10:46
	
	 * mailbox 466180170@qq.com
	 * 转诊：门店退款方式是店转 取金额*系数  加上 转店ID等于大学路金额*系数；
	 */
	
	public function referralMoney($shopId,$shop_type,$startTime,$endTime){
		//门店--转店ID 等于当前门店的ID 
	    
		$receive =Db::name('shop_refund_order')
	
	        ->field('sum(pay_amount * coefficient) as pay_amount,id')
			
	        ->where('referral_shop_id','=',$shopId)
	
	        ->where('type','=',3)
	
	        ->where('status','=',2)
	
	        ->where('refund_type','=','101.02')
	
	        ->when($startTime ?? 0, function ($query) use ($startTime) {
	
	            return $query->whereTime('deal_time', '>=', $startTime);
	
	        })
	
	        ->when($endTime ?? 0, function ($query) use ($endTime) {
	
	            return $query->whereTime('deal_time', '<=', $endTime);
	
	        },function ($query) use ($endTime) {
	
	            return $query->whereMonth('deal_time');
	
	        })->find();
			
		//退款单 店转金额 【判定疗程和非疗程  
		$refund1 = Db::name('shop_refund_order')->alias('r')
	
	        ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
	
	        // ->field('sum(r.pay_amount * r.coefficient) as pay_amount')
	
	        ->where('r.shop_id','=',$shopId)
	        
	        ->where('g.refund_mode','=','06')
	        
	        ->where('r.type','=',1)
	            
	        ->where('r.status','=',2)
	
	        ->where('r.refund_type','<>','101.03')
	
	        ->when($startTime ?? 0, function ($query) use ($startTime) {
	
	            return $query->whereTime('deal_time', '>=', $startTime);
	                
	        })
	
	        ->when($endTime ?? 0, function ($query) use ($endTime) {
	
	            return $query->whereTime('deal_time', '<=', $endTime);
	
	        },function ($query) use ($endTime) {
	
	            return $query->whereMonth('deal_time');
	        
	        })
	        
	        // ->find();
	
	        ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
			
		//退款单 是非店转的 店转金额 
		$refund2 = Db::name('shop_refund_order')->alias('r')
		
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
		
		    // ->field('sum(r.pay_amount * r.coefficient) as pay_amount')
		    
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where('g.refund_mode','<>','06')
		    
		    ->where('r.type','=',1)
		        
		    ->where('r.status','=',2)
			
			->where('r.combo','<>',3)
		    
		    ->where('r.refund_type','<>','101.03')
		    
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		    
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		    
		        return $query->whereTime('deal_time', '<=', $endTime);
		    
		    },function ($query) use ($endTime) {
		    
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    // ->find();
		
		    ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
	
	
		//门诊收款单 是非疗程的 转诊业绩
		$receiveMz =Db::name('shop_refund_order')
		
		    ->field('sum(pay_amount * coefficient) as pay_amount')
		
		    ->where('shop_id','=',$shopId)
	
			->where('is_treatment','=',2)
	
		    ->where('type','=',3) 
	
			->where('referral_shop_id','>',0)
	
		    ->where('status','=',2)
		
		    ->where('refund_type','=','101.02')
		
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		
		        return $query->whereTime('deal_time', '>=', $startTime);
		
		    })
		
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		
		        return $query->whereTime('deal_time', '<=', $endTime);
		
		    },function ($query) use ($endTime) {
		
		        return $query->whereMonth('deal_time');
		
		    })->find();
	
			//})->column('pay_amount * coefficient as pay_amount','id');
			//var_dump($receiveMz);
		//门诊 收款方式是店转 的 店转金额
		$refundMz = Db::name('shop_refund_order')->alias('r')
	
	        ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
	
	        // ->field('sum(r.pay_amount * r.coefficient) as pay_amount')
	
	        ->where('r.shop_id','=',$shopId)
	        
	        ->where('g.refund_mode','=','06')
	        
	        ->where('r.type','=',3)
	            
	        ->where('r.status','=',2)
	
	        ->where('r.refund_type','=','101.02')
	
	        ->when($startTime ?? 0, function ($query) use ($startTime) {
	
	            return $query->whereTime('deal_time', '>=', $startTime);
	                
	        })
	
	        ->when($endTime ?? 0, function ($query) use ($endTime) {
	
	            return $query->whereTime('deal_time', '<=', $endTime);
	
	        },function ($query) use ($endTime) {
	
	            return $query->whereMonth('deal_time');
	        
	        })
	        
	        // ->find();
	        
	        ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
	
	    $refund_sum = array_sum($refund1);
		$refund_sumT = array_sum($refund2);
		
	    $refundMz_sum = array_sum($refundMz);
		
		if($shop_type==1){ //1是门诊 2是门店
		
			return round(bcsub(0,$receiveMz['pay_amount'],2)+$refundMz_sum+bcsub(0,$refund_sum,2)+bcsub(0,$refund_sumT,2),2);
			
		}else{
			
			return round((bcsub($receive['pay_amount'],$refund_sum,2)+$refundMz_sum),2);
	
		}
			
	}
	
	/*
	 * @ 新单金额和补单金额的 计算 逻辑----川渝市场的 新单/补单业绩计算
	 *
	 * @ 门店新单/补单业绩计算 ：收款单（不含店转）的新单/补单 实收金额 【减去】 退款单（不含店转）的新单/补单 实收金额  
	 *                       【加上】  门诊转到门店的 新单/补单 实收金额 【减去】 退款单是店转的 新单/补单金额 【加上】 收款单是店转的新单/补单金额；
	 *
	 * @ 门珍新单/补单业绩计算 ：收款单（不含店转）的新单/补单 实收金额 【减去】 退款单（不含店转）的新单/补单 实收金额  
	 *                       【加上】  非疗程不含店转的 新单/补单 实收金额 【减去】 退款单是店转的 新单/补单金额 【加上】 收款单是店转的新单/补单金额；
	 */
	public function sumShopPayMoneyNew($scene,$shopId,$shop_type,$startTime,$endTime){
		
		switch ($scene) {
			case 'XD':
		
		        $where['order_type']=1;
				$wheret['r.order_type']=1;
				break;
		
		    case 'BD':
		
		        $where['order_type']=2;
				$wheret['r.order_type']=2;
				break;
		
		    default:
		
		        break;
		
		}
		//收款单中的疤痕业绩 不含店转的业绩--01 
		$receive = Db::name('shop_refund_order')->alias('r')
	
	        ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
	        
	        //->field('sum(g.pay_price * r.coefficient) as pay_amount')
			->field('sum(g.pay_price) as pay_amount')
	        ->where('r.shop_id','=',$shopId)
	        
	        ->where($wheret)
	        
	        ->where('g.refund_mode','<>','06')
	        
	        ->where('r.type','=',3)
	            
	        ->where('r.status','=',2)
	
	        ->where('r.refund_type','=','101.02')
	
	        ->when($startTime ?? 0, function ($query) use ($startTime) {
	
	            return $query->whereTime('deal_time', '>=', $startTime);
	                
	        })
	        ->when($endTime ?? 0, function ($query) use ($endTime) {
	
	            return $query->whereTime('deal_time', '<=', $endTime);
	
	        },function ($query) use ($endTime) {
	
	            return $query->whereMonth('deal_time');
	        
	        })
	        
	        ->find();
	
	        // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
	
	
		//退款单中的疤痕业绩	--不含店转的业绩  --02
		$refund = Db::name('shop_refund_order')->alias('r')
	
	        ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
	
	        //->field('sum(g.pay_price * r.coefficient) as pay_amount')
			->field('sum(g.pay_price) as pay_amount')
	        ->where('r.shop_id','=',$shopId)
	        
	        ->where($wheret)
	        
	        ->where('g.refund_mode','<>','06')
	        
	        ->where('r.type','=',1)
	            
	        ->where('r.status','=',2)
	
	        ->where('r.refund_type','=','101.02')
	
	        ->when($startTime ?? 0, function ($query) use ($startTime) {
	
	            return $query->whereTime('deal_time', '>=', $startTime);
	                
	        })
	        ->when($endTime ?? 0, function ($query) use ($endTime) {
	
	            return $query->whereTime('deal_time', '<=', $endTime);
	
	        },function ($query) use ($endTime) {
	
	            return $query->whereMonth('deal_time');
	        
	        })
	        
	        ->find();
	
	        // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
	
	
		//门诊转店到 门店的 通过门店的归属ID 查询	--收款单 --03
		$receive_zd =Db::name('shop_refund_order')
		
		    ->field('sum(pay_amount * coefficient) as pay_amount,id')
	
			->where($where)
	
		    ->where('referral_shop_id','=',$shopId)
			
		    ->where('type','=',3)
		
		    ->where('status','=',2)
		
		    ->where('refund_type','=','101.02')
		
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		
		        return $query->whereTime('deal_time', '>=', $startTime);
		
		    })
		
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		
		        return $query->whereTime('deal_time', '<=', $endTime);
		
		    },function ($query) use ($endTime) {
		
		        return $query->whereMonth('deal_time');
		
		    })->find();	
	
	
		//退款单是店转的 疤痕/项目业绩查询  --04
		$refund_dz = Db::name('shop_refund_order')->alias('r')
	
	        ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
	
	        ->field('sum(g.pay_price * r.coefficient) as pay_amount')
	
	        ->where($wheret)
	
	        ->where('r.shop_id','=',$shopId)
	        
	        ->where('g.refund_mode','=','06')
	        
	        ->where('r.type','=',1)
	            
	        ->where('r.status','=',2)
	
	        ->where('r.refund_type','<>','101.03')
	
	        ->when($startTime ?? 0, function ($query) use ($startTime) {
	
	            return $query->whereTime('deal_time', '>=', $startTime);
	                
	        })
	
	        ->when($endTime ?? 0, function ($query) use ($endTime) {
	
	            return $query->whereTime('deal_time', '<=', $endTime);
	
	        },function ($query) use ($endTime) {
	
	            return $query->whereMonth('deal_time');
	        
	        })
	        
	        ->find();
	
	        // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
	
	
		//收款单是店转的 疤痕/项目业绩---05
		$receive_dz = Db::name('shop_refund_order')->alias('r')
	
	        ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
	
	        ->field('sum(g.pay_price * r.coefficient) as pay_amount')
	    
	        ->where('r.shop_id','=',$shopId)
	        
	        ->where($wheret)
	        
	        ->where('g.refund_mode','=','06')
	        
	        ->where('r.type','=',3)
	            
	        ->where('r.status','=',2)
	
	        ->where('r.refund_type','=','101.02')
	
	        ->when($startTime ?? 0, function ($query) use ($startTime) {
	
	            return $query->whereTime('deal_time', '>=', $startTime);
	                
	        })
	        ->when($endTime ?? 0, function ($query) use ($endTime) {
	
	            return $query->whereTime('deal_time', '<=', $endTime);
	
	        },function ($query) use ($endTime) {
	
	            return $query->whereMonth('deal_time');
	        
	        })
	        
	        ->find();
	
	        // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
	
	
		//门诊收款单 是非疗程的 疤痕/项目业绩  --06
		$receive_mz =Db::name('shop_refund_order')
		
		    ->field('sum(pay_amount * coefficient) as pay_amount')
	
			->where($where)
	
		    ->where('shop_id','=',$shopId) 
	
			->where('is_treatment','=',2)
	        
		    ->where('type','=',3) 
	
			->where('referral_shop_id','>',0)
	
		    ->where('status','=',2)
		
		    ->where('refund_type','<>','101.03')
		
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		
		        return $query->whereTime('deal_time', '>=', $startTime);
		
		    })
		
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		
		        return $query->whereTime('deal_time', '<=', $endTime);
		
		    },function ($query) use ($endTime) {
		
		        return $query->whereMonth('deal_time');
		
		    })->find();
		
		if($shop_type==1){ //1是门诊 2是门店
			//return round($receive['pay_amount']-$refund['pay_amount'] +$receive_dz['pay_amount']+bcsub(0,$receive_mz['pay_amount'],2)+bcsub(0,$refund_dz['pay_amount']),2);
			return round(($receive['pay_amount']-$refund['pay_amount'] +$receive_dz['pay_amount']-$receive_mz['pay_amount']-$refund_dz['pay_amount']),2);
		}else{
			//实收金额 - 退款金额 + 门诊收钱分给店面的一半金额  
			return round(($receive['pay_amount']-$refund['pay_amount'] +$receive_zd['pay_amount']+$receive_dz['pay_amount']-$refund_dz['pay_amount']),2);
		}
	}
	
	
	
	/*
	 * @ 新单和补单的 计算 逻辑----川渝市场的 疤痕和项目业绩计算
	 *
	 * @ 门店新单/补单业绩计算 ：收款单（不含店转）的疤痕/项目 实收金额 【减去】 退款单（不含店转）的疤痕/项目 实收金额  
	 *                       【加上】  门诊转到门店的 疤痕/项目 实收金额 【减去】 退款单是店转的 疤痕/项目金额 【加上】 收款单是店转的疤痕/项目金额；
	 *
	 * @ 门珍新单/补单业绩计算 ：收款单（不含店转）的疤痕/项目 实收金额 【减去】 退款单（不含店转）的疤痕/项目 实收金额  
	 *                       【加上】  非疗程不含店转的 疤痕/项目 实收金额 【减去】 退款单是店转的 疤痕/项目金额 【加上】 收款单是店转的疤痕/项目金额；
	 */
	public function sumShopPayMoneyNewMZ($scene,$shopId,$shop_type,$startTime,$endTime){
		switch ($scene) {
			case 'XD':
		
		        $where['r.order_type']=1;
				break;
		
		    case 'BD':
		
		        $where['r.order_type']=2;
				break;
		
		    default:
		
		        break;
		
		}
		
		//收款单 收款类型是 收款 且不含收款方式不是店转的
		$receive = Db::name('shop_refund_order')->alias('r')
		
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
		    
		    // ->field('sum(r.pay_amount * r.coefficient) as pay_amount')
		    ->where($where)
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where('g.refund_mode','<>','06')
		    
		    ->where('r.type','=',3)
		        
		    ->where('r.status','=',2)
		    
		    ->where('r.refund_type','=','101.02')
		    
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		    
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		    
		        return $query->whereTime('deal_time', '<=', $endTime);
		    
		    },function ($query) use ($endTime) {
		    
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    // ->find();
		
		    ->column('r.pay_amount as pay_amount','r.id');
			//->column('r.pay_amount * r.coefficient as pay_amount','r.id');
		
		//var_dump($receive);
		//退款单	  
		$refund = Db::name('shop_refund_order')->alias('r')
		
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
		    
		    // ->field('sum(r.pay_amount * r.coefficient) as pay_amount')
		    
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where('g.refund_mode','<>','06')
		    
		    ->where('r.type','=',1)
		        
		    ->where('r.status','=',2)
		    
		    ->where('r.refund_type','=','101.02')
		    
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		    
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		    
		        return $query->whereTime('deal_time', '<=', $endTime);
		    
		    },function ($query) use ($endTime) {
		    
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    // ->find();
		
		    //->column('r.pay_amount * r.coefficient as pay_amount','r.id');
			->column('r.pay_amount  as pay_amount','r.id');
		
		// return round($receive['pay_amount'] -$refund['pay_amount'],2);
		
		$receive_sum = array_sum($receive);
		
		$refund_sum = array_sum($refund);
		
		return bcsub($receive_sum,$refund_sum,2);
	}
	
	
	/*
	 * @ 疤痕业绩和项目业绩的 计算 逻辑----川渝市场的 疤痕和项目业绩计算
	 *
	 * @ 门店疤痕/项目业绩计算 ：收款单（不含店转）的疤痕/项目 实收金额 【减去】 退款单（不含店转）的疤痕/项目 实收金额  
	 *                       【加上】  门诊转到门店的 疤痕/项目 实收金额 【减去】 退款单是店转的 疤痕/项目金额 【加上】 收款单是店转的疤痕/项目金额；
	 *
	 * @ 门珍疤痕/项目业绩计算 ：收款单（不含店转）的疤痕/项目 实收金额 【减去】 退款单（不含店转）的疤痕/项目 实收金额  
	 *                       【加上】  非疗程不含店转的 疤痕/项目 实收金额 【减去】 退款单是店转的 疤痕/项目金额 【加上】 收款单是店转的疤痕/项目金额；
	 */
	public function sumShopScarPayMoneyMZ($scene,$shopId,$shop_type,$startTime,$endTime){
		switch ($scene) {
			case 'Bh':
		
		        $where['r.business_type']=1;
				break;
		
		    case 'XM':
		
		        $where['r.business_type']=2;
				break;
		
		    default:
		
		        break;
		
		}
		
		//收款单 收款类型是 收款 且不含收款方式不是店转的
		$receive = Db::name('shop_refund_order')->alias('r')
		
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
		    
		    // ->field('sum(r.pay_amount * r.coefficient) as pay_amount')
		    ->where($where)
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where('g.refund_mode','<>','06')
		    
		    ->where('r.type','=',3)
		        
		    ->where('r.status','=',2)
		    
		    ->where('r.refund_type','=','101.02')
		    
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		    
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		    
		        return $query->whereTime('deal_time', '<=', $endTime);
		    
		    },function ($query) use ($endTime) {
		    
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    // ->find();
		
		    ->column('r.pay_amount as pay_amount','r.id');
			//->column('r.pay_amount * r.coefficient as pay_amount','r.id');
		
		//var_dump($receive);
		//退款单	  
		$refund = Db::name('shop_refund_order')->alias('r')
		
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
		    
		    // ->field('sum(r.pay_amount * r.coefficient) as pay_amount')
		    
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where('g.refund_mode','<>','06')
		    
		    ->where('r.type','=',1)
		        
		    ->where('r.status','=',2)
		    
		    ->where('r.refund_type','=','101.02')
		    
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		    
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		    
		        return $query->whereTime('deal_time', '<=', $endTime);
		    
		    },function ($query) use ($endTime) {
		    
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    // ->find();
		
		    //->column('r.pay_amount * r.coefficient as pay_amount','r.id');
			->column('r.pay_amount  as pay_amount','r.id');
		
		// return round($receive['pay_amount'] -$refund['pay_amount'],2);
		
		$receive_sum = array_sum($receive);
		
		$refund_sum = array_sum($refund);
		
		return bcsub($receive_sum,$refund_sum,2);
	}
	
	
	/* 
	 * @ 疤痕业绩和项目业绩的 计算 逻辑
	 *
	 * @ 门店疤痕/项目业绩计算 ：收款单（不含店转）的疤痕/项目 实收金额 【减去】 退款单（不含店转）的疤痕/项目 实收金额  
	 *                       【加上】  门诊转到门店的 疤痕/项目 实收金额 【减去】 退款单是店转的 疤痕/项目金额 【加上】 收款单是店转的疤痕/项目金额；
	 *
	 * @ 门珍疤痕/项目业绩计算 ：收款单（不含店转）的疤痕/项目 实收金额 【减去】 退款单（不含店转）的疤痕/项目 实收金额  
	 *                       【加上】  非疗程不含店转的 疤痕/项目 实收金额 【减去】 退款单是店转的 疤痕/项目金额 【加上】 收款单是店转的疤痕/项目金额；
	 */
	public function sumShopScarPayMoney($scene,$shopId,$shop_type,$startTime,$endTime){
		//$where =['shop_id'=> $shopId,'type'=>3];
		switch ($scene) {
			case 'Bh':
		
		        $where['business_type']=1;
				$wheret['r.business_type']=1;
				break;
		
		    case 'XM':
		
		        $where['business_type']=2;
				$wheret['r.business_type']=2;
				break;
		
		    default:
		
		        break;
		
		}
		//收款单中的疤痕业绩 不含店转的业绩--01 
		$receive = Db::name('shop_refund_order')->alias('r')
	
	        ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
	        
	        //->field('sum(g.pay_price * r.coefficient) as pay_amount')
			->field('sum(g.pay_price) as pay_amount')
	        ->where('r.shop_id','=',$shopId)
	        
	        ->where($wheret)
	        
	        ->where('g.refund_mode','<>','06')
	        
	        ->where('r.type','=',3)
	            
	        ->where('r.status','=',2)
	
	        ->where('r.refund_type','=','101.02')
	
	        ->when($startTime ?? 0, function ($query) use ($startTime) {
	
	            return $query->whereTime('deal_time', '>=', $startTime);
	                
	        })
	        ->when($endTime ?? 0, function ($query) use ($endTime) {
	
	            return $query->whereTime('deal_time', '<=', $endTime);
	
	        },function ($query) use ($endTime) {
	
	            return $query->whereMonth('deal_time');
	        
	        })
	        
	        ->find();
	
	        // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
	
	
		//退款单中的疤痕业绩	--不含店转的业绩  --02
		$refund = Db::name('shop_refund_order')->alias('r')
	
	        ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
	
	        //->field('sum(g.pay_price * r.coefficient) as pay_amount')
			->field('sum(g.pay_price) as pay_amount')
	        ->where('r.shop_id','=',$shopId)
	        
	        ->where($wheret)
	        
	        ->where('g.refund_mode','<>','06')
	        
	        ->where('r.type','=',1)
	            
	        ->where('r.status','=',2)
	
	        ->where('r.refund_type','=','101.02')
	
	        ->when($startTime ?? 0, function ($query) use ($startTime) {
	
	            return $query->whereTime('deal_time', '>=', $startTime);
	                
	        })
	        ->when($endTime ?? 0, function ($query) use ($endTime) {
	
	            return $query->whereTime('deal_time', '<=', $endTime);
	
	        },function ($query) use ($endTime) {
	
	            return $query->whereMonth('deal_time');
	        
	        })
	        
	        ->find();
	
	        // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
	
	
		//门诊转店到 门店的 通过门店的归属ID 查询	--收款单 --03
		$receive_zd =Db::name('shop_refund_order')
		
		    ->field('sum(pay_amount * coefficient) as pay_amount,id')
	
			->where($where)
	
		    ->where('referral_shop_id','=',$shopId)
			
		    ->where('type','=',3)
		
		    ->where('status','=',2)
		
		    ->where('refund_type','=','101.02')
		
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		
		        return $query->whereTime('deal_time', '>=', $startTime);
		
		    })
		
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		
		        return $query->whereTime('deal_time', '<=', $endTime);
		
		    },function ($query) use ($endTime) {
		
		        return $query->whereMonth('deal_time');
		
		    })->find();	
	
	
		//退款单是店转的 疤痕/项目业绩查询  --04
		$refund_dz = Db::name('shop_refund_order')->alias('r')
	
	        ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
	
	        ->field('sum(g.pay_price * r.coefficient) as pay_amount')
	
	        ->where($wheret)
	
	        ->where('r.shop_id','=',$shopId)
	        
	        ->where('g.refund_mode','=','06')
	        
	        ->where('r.type','=',1)
	            
	        ->where('r.status','=',2)
	
	        ->where('r.refund_type','<>','101.03')
	
	        ->when($startTime ?? 0, function ($query) use ($startTime) {
	
	            return $query->whereTime('deal_time', '>=', $startTime);
	                
	        })
	
	        ->when($endTime ?? 0, function ($query) use ($endTime) {
	
	            return $query->whereTime('deal_time', '<=', $endTime);
	
	        },function ($query) use ($endTime) {
	
	            return $query->whereMonth('deal_time');
	        
	        })
	        
	        ->find();
	
	        // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
	
	
		//收款单是店转的 疤痕/项目业绩---05
		$receive_dz = Db::name('shop_refund_order')->alias('r')
	
	        ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
	
	        ->field('sum(g.pay_price * r.coefficient) as pay_amount')
	    
	        ->where('r.shop_id','=',$shopId)
	        
	        ->where($wheret)
	        
	        ->where('g.refund_mode','=','06')
	        
	        ->where('r.type','=',3)
	            
	        ->where('r.status','=',2)
	
	        ->where('r.refund_type','=','101.02')
	
	        ->when($startTime ?? 0, function ($query) use ($startTime) {
	
	            return $query->whereTime('deal_time', '>=', $startTime);
	                
	        })
	        ->when($endTime ?? 0, function ($query) use ($endTime) {
	
	            return $query->whereTime('deal_time', '<=', $endTime);
	
	        },function ($query) use ($endTime) {
	
	            return $query->whereMonth('deal_time');
	        
	        })
	        
	        ->find();
	
	        // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
	
	
		//门诊收款单 是非疗程的 疤痕/项目业绩  --06
		$receive_mz =Db::name('shop_refund_order')
		
		    ->field('sum(pay_amount * coefficient) as pay_amount')
	
			->where($where)
	
		    ->where('shop_id','=',$shopId) 
	
			->where('is_treatment','=',2)
	        
		    ->where('type','=',3) 
	
			->where('referral_shop_id','>',0)
	
		    ->where('status','=',2)
		
		    ->where('refund_type','<>','101.03')
		
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		
		        return $query->whereTime('deal_time', '>=', $startTime);
		
		    })
		
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		
		        return $query->whereTime('deal_time', '<=', $endTime);
		
		    },function ($query) use ($endTime) {
		
		        return $query->whereMonth('deal_time');
		
		    })->find();
		
		if($shop_type==1){ //1是门诊 2是门店
			//return round($receive['pay_amount']-$refund['pay_amount'] +$receive_dz['pay_amount']+bcsub(0,$receive_mz['pay_amount'],2)+bcsub(0,$refund_dz['pay_amount']),2);
			return round(($receive['pay_amount']-$refund['pay_amount'] +$receive_dz['pay_amount']-$receive_mz['pay_amount']-$refund_dz['pay_amount']),2);
		}else{
			//实收金额 - 退款金额 + 门诊收钱分给店面的一半金额  
			return round(($receive['pay_amount']-$refund['pay_amount'] +$receive_zd['pay_amount']+$receive_dz['pay_amount']-$refund_dz['pay_amount']),2);
		}
	}
	
	
}

